﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'ufn_UPDMS_Get_Date_Add')
	BEGIN
		DROP Function ufn_UPDMS_Get_Date_Add
	END
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
/******************************************************************************
**	Name : ufn_UPDMS_Get_Date_Add
**	Desc : 날짜계산 (이전, 이후)
**	Test Exec Query : select dbo.ufn_UPDMS_Get_Date_Add ('D', -1, '2011-01-01')
**	Called by : 전체
**	Program ID : 전체
**	Auth : 송시명
**	Date : 2011-03-11
*******************************************************************************
**	Change History
*******************************************************************************
**	Date:		Author:		Description:
**	--------	--------	---------------------------------------
**	
*******************************************************************************/
CREATE FUNCTION [dbo].[ufn_UPDMS_Get_Date_Add]
(
@ls_div nvarchar(1),
@li_add int,
@ls_date nvarchar(10)
)
RETURNS nvarchar(10)
AS

BEGIN
    DECLARE @rtn nvarchar(10)
    SET @rtn = ''
		   
    IF @ls_div = 'D'
    BEGIN
        SELECT @rtn = LEFT(CONVERT(NVARCHAR, DATEADD(D, @li_add, CONVERT(DATETIME, @ls_date)),121),10)
    END
    ELSE IF @ls_div = 'M'
    BEGIN
        SELECT @rtn = LEFT(CONVERT(NVARCHAR, DATEADD(M, @li_add, CONVERT(DATETIME, @ls_date)),121),10)
    END
    ELSE
    BEGIN
        SELECT @rtn = LEFT(CONVERT(NVARCHAR, GETDATE(), 121),10)
    END

    RETURN LTRIM(RTRIM(@rtn))
  
END